<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/Peppa.png?v=7.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/Peppa.jpg?v=7.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/Peppa.jpg?v=7.0.0">








<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.0.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="1 任务描述1.1 概述使用java或C/C++编程语言，独立完成一个3到5个运算符的四则运算练习的软件 1.2 基本要求 1 程序可接收一个输入参数n，然后随机产生n道加减乘除（分别使用符号+-*÷来表示）练习题，每个数字在 0 和 100 之间，运算符在3个到5个之间。  2 每个练习题至少要包含2种运算符。同时，由于小学生没有分数与负数的概念，你所出的练习题在运算过程中不得出现负数与非整数，">
<meta name="keywords" content="软件工程导论">
<meta property="og:type" content="article">
<meta property="og:title" content="Arithmetic Device">
<meta property="og:url" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/index.html">
<meta property="og:site_name" content="linxi&#39;s dream">
<meta property="og:description" content="1 任务描述1.1 概述使用java或C/C++编程语言，独立完成一个3到5个运算符的四则运算练习的软件 1.2 基本要求 1 程序可接收一个输入参数n，然后随机产生n道加减乘除（分别使用符号+-*÷来表示）练习题，每个数字在 0 和 100 之间，运算符在3个到5个之间。  2 每个练习题至少要包含2种运算符。同时，由于小学生没有分数与负数的概念，你所出的练习题在运算过程中不得出现负数与非整数，">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p1.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p2.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p3.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p4.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p5.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p6.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p7.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p8.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p9.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p10.png">
<meta property="og:updated_time" content="2019-12-25T01:29:16.114Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Arithmetic Device">
<meta name="twitter:description" content="1 任务描述1.1 概述使用java或C/C++编程语言，独立完成一个3到5个运算符的四则运算练习的软件 1.2 基本要求 1 程序可接收一个输入参数n，然后随机产生n道加减乘除（分别使用符号+-*÷来表示）练习题，每个数字在 0 和 100 之间，运算符在3个到5个之间。  2 每个练习题至少要包含2种运算符。同时，由于小学生没有分数与负数的概念，你所出的练习题在运算过程中不得出现负数与非整数，">
<meta name="twitter:image" content="https://linxi99.gitee.io/20190319/Arithmetic-Device/p1.png">






  <link rel="canonical" href="https://linxi99.gitee.io/20190319/Arithmetic-Device/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Arithmetic Device | linxi's dream</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b4099cd89c9f33abab06a03748dac801";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">linxi's dream</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">穷且益坚，不坠青云之志</p>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://linxi99.gitee.io/20190319/Arithmetic-Device/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="linxi">
      <meta itemprop="description" content="stay hungry, stay foolish">
      <meta itemprop="image" content="/images/star.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="linxi's dream">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Arithmetic Device

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-03-19 12:09:41" itemprop="dateCreated datePublished" datetime="2019-03-19T12:09:41+08:00">2019-03-19</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-12-25 09:29:16" itemprop="dateModified" datetime="2019-12-25T09:29:16+08:00">2019-12-25</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/软件工程导论/" itemprop="url" rel="index"><span itemprop="name">软件工程导论</span></a></span>

                
                
              
            </span>
          

          
            
            
              
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
            
                <span class="post-meta-item-text">评论数：</span>
                <a href="/20190319/Arithmetic-Device/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/20190319/Arithmetic-Device/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
            <span id="/20190319/Arithmetic-Device/" class="leancloud_visitors" data-flag-title="Arithmetic Device">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              
                <span class="post-meta-item-text">阅读次数：</span>
              
                <span class="leancloud-visitors-count"></span>
            </span>
            <span class="post-meta-divider">|
            </span> <span title="post.wordcount">
            <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
            </span>字数： 7.5k
            </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="1-任务描述"><a href="#1-任务描述" class="headerlink" title="1 任务描述"></a>1 任务描述</h1><h2 id="1-1-概述"><a href="#1-1-概述" class="headerlink" title="1.1 概述"></a>1.1 概述</h2><p>使用java或C/C++编程语言，独立完成一个3到5个运算符的四则运算练习的软件</p>
<h2 id="1-2-基本要求"><a href="#1-2-基本要求" class="headerlink" title="1.2 基本要求"></a>1.2 基本要求</h2><ul>
<li><p>1 程序可接收一个输入参数n，然后随机产生n道加减乘除（分别使用符号+-*÷来表示）练习题，每个数字在 0 和 100 之间，运算符在3个到5个之间。</p>
</li>
<li><p>2 每个练习题至少要包含2种运算符。同时，由于小学生没有分数与负数的概念，你所出的练习题在运算过程中不得出现负数与非整数，比如不能出 3÷5+2=2.6，2-5+10=7等算式。</p>
</li>
<li><p>3 练习题生成好后，将你的学号与生成的n道练习题及其对应的正确答案输出到文件“result.txt”中，不要输出额外信息，文件目录与程序目录一致。</p>
</li>
<li><p>4 当程序接收的参数为4时，以下为一个输出文件示例。</p>
</li>
</ul>
<pre><code>    2018010203
    13+17-1=29
    11*15-5=160
    3+10+4-16=1
    15÷5+3-2=4
</code></pre><a id="more"></a>
<h2 id="1-3-附加功能要求"><a href="#1-3-附加功能要求" class="headerlink" title="1.3 附加功能要求"></a>1.3 附加功能要求</h2><ul>
<li><p>1 支持有括号的运算式，包括出题与求解正确答案。注意，算式中存在的括号数必须大于2对，且不得超过运算符的个数。</p>
</li>
<li><p>2 扩展程序功能支持真分数的出题与运算（只需要涵盖加减法即可），例如：1/6 + 1/8 + 2/3= 23/24。注意在实现本功能时，需支持运算时分数的自动化简，比如 1/2+1/6=2/3，而非4/6，且计算过程中与结果都须为真分数。</p>
</li>
</ul>
<h1 id="2-项目地址"><a href="#2-项目地址" class="headerlink" title="2 项目地址"></a>2 项目地址</h1><p>个人博客 </p>
<p><a href="https://linxi99.gitee.io/">https://linxi99.gitee.io/</a></p>
<p><a href="https://linxi99.gitee.io/20190319/Arithmetic-Device/">https://linxi99.gitee.io/20190319/Arithmetic-Device/</a></p>
<p><a href="https://blog.csdn.net/linxilinxilinxi" target="_blank" rel="noopener">https://blog.csdn.net/linxilinxilinxi</a></p>
<p><a href="https://blog.csdn.net/linxilinxilinxi/article/details/88548481" target="_blank" rel="noopener">https://blog.csdn.net/linxilinxilinxi/article/details/88548481</a></p>
<p>项目地址 </p>
<p><a href="https://gitee.com/linxi99/four_operational_generators" target="_blank" rel="noopener">https://gitee.com/linxi99/four_operational_generators</a></p>
<h1 id="3-项目源代码"><a href="#3-项目源代码" class="headerlink" title="3 项目源代码"></a>3 项目源代码</h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function">mt19937 <span class="title">mt</span><span class="params">(time(<span class="number">0</span>))</span></span>;</span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">char</span>&gt; op;</span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; pri;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> id, val;</span><br><span class="line">    node(<span class="keyword">int</span> id = <span class="number">-1</span>, <span class="keyword">int</span> val = <span class="number">-1</span>):id(id), val(val)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> tot, operatorNum, operandNum, bracketNum, ans;</span><br><span class="line"><span class="keyword">int</span> hasBracket[<span class="number">15</span>], operators[<span class="number">15</span>], operands[<span class="number">15</span>];</span><br><span class="line"><span class="keyword">int</span> RPN[<span class="number">55</span>], fac[<span class="number">105</span>];</span><br><span class="line"><span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; opr;</span><br><span class="line"><span class="built_in">stack</span>&lt;node&gt; opd;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init1</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">15</span>; ++i)&#123;</span><br><span class="line">        operators[i] = <span class="number">-1</span>;</span><br><span class="line">        operands[i] = <span class="number">-1</span>;</span><br><span class="line">        hasBracket[i] = <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init2</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">getOperands</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init2();</span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) opr.push(hasBracket[<span class="number">0</span>]);</span><br><span class="line">    RPN[tot++] = <span class="number">0</span>;</span><br><span class="line"><span class="comment">//    puts("**3.1**");</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line"><span class="comment">//        printf("%d\n", i);</span></span><br><span class="line">        <span class="keyword">while</span>(<span class="literal">true</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(opr.empty() || opr.top() == <span class="number">14</span> || pri[operators[i - <span class="number">1</span>]] &gt; pri[opr.top()])&#123;</span><br><span class="line">                opr.push(operators[i - <span class="number">1</span>]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            RPN[tot++] = opr.top();</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>)&#123;</span><br><span class="line">            opr.push(hasBracket[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        RPN[tot++] = i;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>)&#123;</span><br><span class="line">            <span class="keyword">while</span>(opr.top() != <span class="number">14</span>)&#123;</span><br><span class="line">                RPN[tot++] = opr.top();</span><br><span class="line">                opr.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    puts("**3.2**");</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span>(!opr.empty())&#123;</span><br><span class="line">        RPN[tot++] = opr.top();</span><br><span class="line">        opr.pop();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    for(int i = 0; i &lt; tot; ++i)&#123;</span></span><br><span class="line"><span class="comment">//        printf("%d ", RPN[i]);</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"><span class="comment">//    puts("");</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//    puts("**3.3**");</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; tot; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(RPN[i] &lt; <span class="number">10</span>)&#123;</span><br><span class="line">            <span class="keyword">int</span> x = mt()%<span class="number">66</span> + <span class="number">1</span>;</span><br><span class="line">            operands[RPN[i]] = x;</span><br><span class="line">            opd.push(node(RPN[i], x));</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(RPN[i] == <span class="number">13</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(a.val%b.val == <span class="number">0</span>)&#123;</span><br><span class="line">                opd.push(node(<span class="number">-1</span>, a.val/b.val));</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(b.id == <span class="number">-1</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">int</span> cnt = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j &lt;= a.val; ++j)&#123;</span><br><span class="line">                <span class="keyword">if</span>(a.val%j) <span class="keyword">continue</span>;</span><br><span class="line">                fac[cnt++] = j;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">int</span> x = mt()%cnt;</span><br><span class="line">            operands[b.id] = fac[x];</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, a.val/fac[x]));</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(RPN[i] == <span class="number">11</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">int</span> dt = a.val - b.val;</span><br><span class="line">            <span class="keyword">if</span>(dt &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, dt));</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">10</span>) opd.push(node(<span class="number">-1</span>, a.val + b.val));</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">12</span>) opd.push(node(<span class="number">-1</span>, a.val*b.val));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    puts("**3.4**");</span></span><br><span class="line"></span><br><span class="line">    ans = opd.top().val; opd.pop();</span><br><span class="line">    <span class="keyword">if</span>(ans &lt; <span class="number">0</span> || ans &gt; <span class="number">1000</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"><span class="comment">//    for(int i = 0; i &lt; tot; ++i)&#123;</span></span><br><span class="line"><span class="comment">//        printf("%d ", RPN[i]);</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"><span class="comment">//    puts("");</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">solve</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init1();</span><br><span class="line">    operatorNum = mt()%<span class="number">3</span> + <span class="number">3</span>;</span><br><span class="line">    operandNum = operatorNum + <span class="number">1</span>;</span><br><span class="line">    bracketNum = min((<span class="keyword">int</span>)(operandNum/<span class="number">2</span>), (<span class="keyword">int</span>)(mt()%<span class="number">2</span> + <span class="number">2</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//    printf("%d %d %d\n", operatorNum, operandNum, bracketNum);</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operatorNum; ++i) operators[i] = mt()%<span class="number">4</span> + <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; bracketNum*<span class="number">2</span>; ++i)&#123;</span><br><span class="line">        <span class="keyword">int</span> x = mt()%operandNum;</span><br><span class="line">        <span class="keyword">while</span>(hasBracket[x] != <span class="number">-1</span>) x = mt()%operandNum;</span><br><span class="line">        hasBracket[x] = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    puts("**1**");</span></span><br><span class="line">    <span class="keyword">bool</span> lf = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">13</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>)&#123;</span><br><span class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">15</span>; ++j) hasBracket[j] = <span class="number">-1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(i &lt; operandNum - <span class="number">1</span> &amp;&amp; operators[i] &gt;= <span class="number">12</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">//    puts("**2**");</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">-1</span>) <span class="keyword">continue</span>;</span><br><span class="line">        <span class="keyword">if</span>(lf) hasBracket[i] = <span class="number">14</span>;</span><br><span class="line">        <span class="keyword">else</span> hasBracket[i] = <span class="number">15</span>;</span><br><span class="line">        lf = (!lf);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    puts("**3**");</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(!getOperands()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    puts("**4**");</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">13</span>) <span class="built_in">printf</span>(<span class="string">""</span>);</span><br><span class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%c"</span>, op[operators[i - <span class="number">1</span>]]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[i]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>) <span class="built_in">printf</span>(<span class="string">")"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"=%d\n"</span>, ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    op[<span class="number">10</span>] = <span class="string">'+'</span>, op[<span class="number">11</span>] = <span class="string">'-'</span>;</span><br><span class="line">    op[<span class="number">12</span>] = <span class="string">'*'</span>;</span><br><span class="line">    op[<span class="number">14</span>] = <span class="string">'('</span>, op[<span class="number">15</span>] = <span class="string">')'</span>;</span><br><span class="line">    pri[<span class="number">10</span>] = <span class="number">0</span>, pri[<span class="number">11</span>] = <span class="number">0</span>;</span><br><span class="line">    pri[<span class="number">12</span>] = <span class="number">1</span>, pri[<span class="number">13</span>] = <span class="number">1</span>;</span><br><span class="line">    pri[<span class="number">14</span>] = <span class="number">2</span>, pri[<span class="number">15</span>] = <span class="number">2</span>;</span><br><span class="line">    freopen(<span class="string">"../result.txt"</span>,<span class="string">"w"</span>,<span class="built_in">stdout</span>);</span><br><span class="line">    <span class="keyword">int</span> n;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">puts</span>(<span class="string">"2017012449"</span>);</span><br><span class="line">    <span class="keyword">while</span>(n--) <span class="keyword">while</span>(!solve());</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="4-更新版本"><a href="#4-更新版本" class="headerlink" title="4 更新版本"></a>4 更新版本</h1><h2 id="4-1-源代码-2-0"><a href="#4-1-源代码-2-0" class="headerlink" title="4.1 源代码 2.0"></a>4.1 源代码 2.0</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function">mt19937 <span class="title">mt</span><span class="params">(time(<span class="number">0</span>))</span></span>; <span class="comment">// 随机数生成器</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">char</span>&gt; op; <span class="comment">// 运算符id及其符号映射</span></span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; pri; <span class="comment">// 运算符id及其优先级映射</span></span><br><span class="line"><span class="comment">// 10表示加号   运算优先级为 0</span></span><br><span class="line"><span class="comment">// 11表示减号   运算优先级为 0</span></span><br><span class="line"><span class="comment">// 12表示乘号   运算优先级为 1</span></span><br><span class="line"><span class="comment">// 13表示除号   运算优先级为 1</span></span><br><span class="line"><span class="comment">// 14表示左括号 运算优先级为 2</span></span><br><span class="line"><span class="comment">// 15表示右括号 运算优先级为 2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 运算数结构体，id为运算数的位置，val为运算数的值</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> id, val;</span><br><span class="line">    node(<span class="keyword">int</span> id = <span class="number">-1</span>, <span class="keyword">int</span> val = <span class="number">-1</span>):id(id), val(val)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// tot：逆波兰表达式的长度，operatorNum：运算符个数</span></span><br><span class="line"><span class="comment">// operandNum：运算数的个数，bracketNum：括号对数，ans：运算式最终答案</span></span><br><span class="line"><span class="comment">// hasBracket：某个数字处是否有括号，-1表示无括号，14表示左括号，15表示右括号</span></span><br><span class="line"><span class="comment">// operators：对应位置的运算符种类 operands：表示对应位置运算数的值</span></span><br><span class="line"><span class="comment">// RPN：存储逆波兰表达式 fac：存储某个数的约数</span></span><br><span class="line"><span class="comment">// opr：中缀表达式转后缀表达式时的运算符栈</span></span><br><span class="line"><span class="comment">// opd：中缀表达式转后缀表达式时的运算数栈</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> tot, operatorNum, operandNum, bracketNum, ans;</span><br><span class="line"><span class="keyword">int</span> hasBracket[<span class="number">15</span>], operators[<span class="number">15</span>], operands[<span class="number">15</span>];</span><br><span class="line"><span class="keyword">int</span> RPN[<span class="number">55</span>], fac[<span class="number">105</span>];</span><br><span class="line"><span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; opr;</span><br><span class="line"><span class="built_in">stack</span>&lt;node&gt; opd;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数零：初始化运算符 id 符号 以及运算优先级，并且重定向输出</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init0</span><span class="params">()</span></span>&#123;</span><br><span class="line">    op[<span class="number">10</span>] = <span class="string">'+'</span>, op[<span class="number">11</span>] = <span class="string">'-'</span>;</span><br><span class="line">    op[<span class="number">12</span>] = <span class="string">'*'</span>;</span><br><span class="line">    op[<span class="number">14</span>] = <span class="string">'('</span>, op[<span class="number">15</span>] = <span class="string">')'</span>;</span><br><span class="line">    pri[<span class="number">10</span>] = <span class="number">0</span>, pri[<span class="number">11</span>] = <span class="number">0</span>;</span><br><span class="line">    pri[<span class="number">12</span>] = <span class="number">1</span>, pri[<span class="number">13</span>] = <span class="number">1</span>;</span><br><span class="line">    pri[<span class="number">14</span>] = <span class="number">2</span>, pri[<span class="number">15</span>] = <span class="number">2</span>;</span><br><span class="line">    freopen(<span class="string">"../result.txt"</span>, <span class="string">"w"</span>, <span class="built_in">stdout</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数一：在每次调用solve()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init1</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">15</span>; ++i)&#123;</span><br><span class="line">        operators[i] = <span class="number">-1</span>;</span><br><span class="line">        operands[i] = <span class="number">-1</span>;</span><br><span class="line">        hasBracket[i] = <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数二：在每次调用getOperands()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init2</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取运算数的值的函数，如果返回 true 则表示获取成功，否则表示获取失败</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">getOperands</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init2();</span><br><span class="line">    <span class="comment">// 将中缀表达式转换成后缀表达式（也就是逆波兰表达式）</span></span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) opr.push(hasBracket[<span class="number">0</span>]);</span><br><span class="line">    RPN[tot++] = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">while</span>(<span class="literal">true</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(opr.empty() || opr.top() == <span class="number">14</span> || pri[operators[i - <span class="number">1</span>]] &gt; pri[opr.top()])&#123;</span><br><span class="line">                opr.push(operators[i - <span class="number">1</span>]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            RPN[tot++] = opr.top();</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>)&#123;</span><br><span class="line">            opr.push(hasBracket[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        RPN[tot++] = i;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>)&#123;</span><br><span class="line">            <span class="keyword">while</span>(opr.top() != <span class="number">14</span>)&#123;</span><br><span class="line">                RPN[tot++] = opr.top();</span><br><span class="line">                opr.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty())&#123;</span><br><span class="line">        RPN[tot++] = opr.top();</span><br><span class="line">        opr.pop();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 转换成逆波兰表达式后便可以进行尝试填数</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; tot; ++i)&#123;</span><br><span class="line">        <span class="comment">// 如果为运算数则随机为其赋值</span></span><br><span class="line">        <span class="keyword">if</span>(RPN[i] &lt; <span class="number">10</span>)&#123;</span><br><span class="line">            <span class="keyword">int</span> x = mt()%<span class="number">66</span> + <span class="number">1</span>;</span><br><span class="line">            operands[RPN[i]] = x;</span><br><span class="line">            opd.push(node(RPN[i], x));</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//如果为除法，要将除数随机分配为被除数的一个因子</span></span><br><span class="line">        <span class="comment">//如果为减法，要注意减数不能大于被减数</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(RPN[i] == <span class="number">13</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(a.val%b.val == <span class="number">0</span>)&#123;</span><br><span class="line">                opd.push(node(<span class="number">-1</span>, a.val/b.val));</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(b.id == <span class="number">-1</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">int</span> cnt = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j &lt;= a.val; ++j)&#123;</span><br><span class="line">                <span class="keyword">if</span>(a.val%j) <span class="keyword">continue</span>;</span><br><span class="line">                fac[cnt++] = j;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">int</span> x = mt()%cnt;</span><br><span class="line">            operands[b.id] = fac[x];</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, a.val/fac[x]));</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(RPN[i] == <span class="number">11</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">int</span> dt = a.val - b.val;</span><br><span class="line">            <span class="keyword">if</span>(dt &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, dt));</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">10</span>) opd.push(node(<span class="number">-1</span>, a.val + b.val));</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">12</span>) opd.push(node(<span class="number">-1</span>, a.val*b.val));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    ans = opd.top().val; opd.pop();</span><br><span class="line">    <span class="comment">//控制最终运算结果的范围，可根据需要进行调节</span></span><br><span class="line">    <span class="keyword">if</span>(ans &lt; <span class="number">0</span> || ans &gt; <span class="number">1000</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">solve</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init1();</span><br><span class="line">    <span class="comment">// 随机生成运算符的个数 3~5 ，及运算数个数 4~6</span></span><br><span class="line">    operatorNum = mt()%<span class="number">3</span> + <span class="number">3</span>;</span><br><span class="line">    operandNum = operatorNum + <span class="number">1</span>;</span><br><span class="line">    <span class="comment">// 随机生成括号个数</span></span><br><span class="line">    bracketNum = min((<span class="keyword">int</span>)(operandNum/<span class="number">2</span>), (<span class="keyword">int</span>)(mt()%<span class="number">3</span> + <span class="number">2</span>));</span><br><span class="line">    <span class="comment">// 随机生成运算符的种类</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operatorNum; ++i) operators[i] = mt()%<span class="number">4</span> + <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 随机生成括号位置</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; bracketNum*<span class="number">2</span>; ++i)&#123;</span><br><span class="line">        <span class="keyword">int</span> x = mt()%operandNum;</span><br><span class="line">        <span class="keyword">while</span>(hasBracket[x] != <span class="number">-1</span>) x = mt()%operandNum;</span><br><span class="line">        hasBracket[x] = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据相对位置确定括号为左括号还是右括号</span></span><br><span class="line">    <span class="keyword">bool</span> lf = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">-1</span>) <span class="keyword">continue</span>;</span><br><span class="line">        <span class="keyword">if</span>(lf) hasBracket[i] = <span class="number">14</span>;</span><br><span class="line">        <span class="keyword">else</span> hasBracket[i] = <span class="number">15</span>;</span><br><span class="line">        lf = (!lf);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 到这里已经将等式预处理成了(a+b)/c*(d-e)的类似形式</span></span><br><span class="line">    <span class="comment">// 预处理结束之后，我们就要尝试将 a b c d e 确定为具体的数</span></span><br><span class="line">    <span class="keyword">if</span>(!getOperands()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在获取完运算数之后，便可以输出我们得到的等式了</span></span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">13</span>) <span class="built_in">printf</span>(<span class="string">"÷"</span>);</span><br><span class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%c"</span>, op[operators[i - <span class="number">1</span>]]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[i]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>) <span class="built_in">printf</span>(<span class="string">")"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"=%d\n"</span>, ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">// 初始化函数零：初始化运算符 id 符号 以及运算优先级，并且重定向输出</span></span><br><span class="line">    init0();</span><br><span class="line">    <span class="comment">//读入需要生成的运算式数量</span></span><br><span class="line">    <span class="keyword">int</span> n;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);</span><br><span class="line">    <span class="comment">//输出学号及运算式</span></span><br><span class="line">    <span class="built_in">puts</span>(<span class="string">"2017012449"</span>);</span><br><span class="line">    <span class="keyword">while</span>(n--) <span class="keyword">while</span>(!solve()) ;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-2-源代码-3-0"><a href="#4-2-源代码-3-0" class="headerlink" title="4.2 源代码 3.0"></a>4.2 源代码 3.0</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function">mt19937 <span class="title">mt</span><span class="params">(time(<span class="number">0</span>))</span></span>; <span class="comment">// 随机数生成器</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">char</span>&gt; op; <span class="comment">// 运算符id及其符号映射</span></span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; pri; <span class="comment">// 运算符id及其优先级映射</span></span><br><span class="line"><span class="comment">// 10表示加号   运算优先级为 0</span></span><br><span class="line"><span class="comment">// 11表示减号   运算优先级为 0</span></span><br><span class="line"><span class="comment">// 12表示乘号   运算优先级为 1</span></span><br><span class="line"><span class="comment">// 13表示除号   运算优先级为 1</span></span><br><span class="line"><span class="comment">// 14表示左括号 运算优先级为 2</span></span><br><span class="line"><span class="comment">// 15表示右括号 运算优先级为 2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 运算数结构体，id为运算数的位置，val为运算数的值</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> id, val;</span><br><span class="line">    node(<span class="keyword">int</span> id = <span class="number">-1</span>, <span class="keyword">int</span> val = <span class="number">-1</span>):id(id), val(val)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// tot：逆波兰表达式的长度，operatorNum：运算符个数</span></span><br><span class="line"><span class="comment">// operandNum：运算数的个数，bracketNum：括号对数，ans：运算式最终答案</span></span><br><span class="line"><span class="comment">// hasBracket：某个数字处是否有括号，-1表示无括号，14表示左括号，15表示右括号</span></span><br><span class="line"><span class="comment">// operators：对应位置的运算符种类 operands：表示对应位置运算数的值</span></span><br><span class="line"><span class="comment">// RPN：存储逆波兰表达式 fac：存储某个数的约数</span></span><br><span class="line"><span class="comment">// opr：中缀表达式转后缀表达式时的运算符栈</span></span><br><span class="line"><span class="comment">// opd：中缀表达式转后缀表达式时的运算数栈</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> tot, operatorNum, operandNum, bracketNum, ans;</span><br><span class="line"><span class="keyword">int</span> hasBracket[<span class="number">15</span>], operators[<span class="number">15</span>], operands[<span class="number">15</span>];</span><br><span class="line"><span class="keyword">int</span> RPN[<span class="number">55</span>], fac[<span class="number">105</span>];</span><br><span class="line"><span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; opr;</span><br><span class="line"><span class="built_in">stack</span>&lt;node&gt; opd;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数零：初始化运算符 id 符号 以及运算优先级，并且重定向输出</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init0</span><span class="params">()</span></span>&#123;</span><br><span class="line">    op[<span class="number">10</span>] = <span class="string">'+'</span>, op[<span class="number">11</span>] = <span class="string">'-'</span>;</span><br><span class="line">    op[<span class="number">12</span>] = <span class="string">'*'</span>;</span><br><span class="line">    op[<span class="number">14</span>] = <span class="string">'('</span>, op[<span class="number">15</span>] = <span class="string">')'</span>;</span><br><span class="line">    pri[<span class="number">10</span>] = <span class="number">0</span>, pri[<span class="number">11</span>] = <span class="number">0</span>;</span><br><span class="line">    pri[<span class="number">12</span>] = <span class="number">1</span>, pri[<span class="number">13</span>] = <span class="number">1</span>;</span><br><span class="line">    pri[<span class="number">14</span>] = <span class="number">2</span>, pri[<span class="number">15</span>] = <span class="number">2</span>;</span><br><span class="line">    freopen(<span class="string">"../result.txt"</span>, <span class="string">"w"</span>, <span class="built_in">stdout</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数一：在每次调用solve()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init1</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">15</span>; ++i)&#123;</span><br><span class="line">        operators[i] = <span class="number">-1</span>;</span><br><span class="line">        operands[i] = <span class="number">-1</span>;</span><br><span class="line">        hasBracket[i] = <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数二：在每次调用getOperands()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init2</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取运算数的值的函数，如果返回 true 则表示获取成功，否则表示获取失败</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">getOperands</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init2();</span><br><span class="line">    <span class="comment">// 将中缀表达式转换成后缀表达式（也就是逆波兰表达式）</span></span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) opr.push(hasBracket[<span class="number">0</span>]);</span><br><span class="line">    RPN[tot++] = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">while</span>(<span class="literal">true</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(opr.empty() || opr.top() == <span class="number">14</span> || pri[operators[i - <span class="number">1</span>]] &gt; pri[opr.top()])&#123;</span><br><span class="line">                opr.push(operators[i - <span class="number">1</span>]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            RPN[tot++] = opr.top();</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>)&#123;</span><br><span class="line">            opr.push(hasBracket[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        RPN[tot++] = i;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>)&#123;</span><br><span class="line">            <span class="keyword">while</span>(opr.top() != <span class="number">14</span>)&#123;</span><br><span class="line">                RPN[tot++] = opr.top();</span><br><span class="line">                opr.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty())&#123;</span><br><span class="line">        RPN[tot++] = opr.top();</span><br><span class="line">        opr.pop();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 转换成逆波兰表达式后便可以进行尝试填数</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; tot; ++i)&#123;</span><br><span class="line">        <span class="comment">// 如果为运算数则随机为其赋值</span></span><br><span class="line">        <span class="keyword">if</span>(RPN[i] &lt; <span class="number">10</span>)&#123;</span><br><span class="line">            <span class="keyword">int</span> x = mt()%<span class="number">66</span> + <span class="number">1</span>;</span><br><span class="line">            operands[RPN[i]] = x;</span><br><span class="line">            opd.push(node(RPN[i], x));</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//如果为除法，要将除数随机分配为被除数的一个因子</span></span><br><span class="line">        <span class="comment">//如果为减法，要注意减数不能大于被减数</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(RPN[i] == <span class="number">13</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(a.val%b.val == <span class="number">0</span>)&#123;</span><br><span class="line">                opd.push(node(<span class="number">-1</span>, a.val/b.val));</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(b.id == <span class="number">-1</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">int</span> cnt = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j &lt;= a.val; ++j)&#123;</span><br><span class="line">                <span class="keyword">if</span>(j &gt;= <span class="number">100</span>) <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">if</span>(a.val%j) <span class="keyword">continue</span>;</span><br><span class="line">                fac[cnt++] = j;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">int</span> x = mt()%cnt;</span><br><span class="line">            operands[b.id] = fac[x];</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, a.val/fac[x]));</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(RPN[i] == <span class="number">11</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">int</span> dt = a.val - b.val;</span><br><span class="line">            <span class="keyword">if</span>(dt &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, dt));</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">10</span>) opd.push(node(<span class="number">-1</span>, a.val + b.val));</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">12</span>) opd.push(node(<span class="number">-1</span>, a.val*b.val));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    ans = opd.top().val; opd.pop();</span><br><span class="line">    <span class="comment">//控制最终运算结果的范围，可根据需要进行调节</span></span><br><span class="line">    <span class="keyword">if</span>(ans &lt; <span class="number">0</span> || ans &gt; <span class="number">1000</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">solve</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init1();</span><br><span class="line">    <span class="comment">// 随机生成运算符的个数 3~5 ，及运算数个数 4~6</span></span><br><span class="line">    operatorNum = mt()%<span class="number">3</span> + <span class="number">3</span>;</span><br><span class="line">    operandNum = operatorNum + <span class="number">1</span>;</span><br><span class="line">    <span class="comment">// 随机生成括号个数</span></span><br><span class="line">    bracketNum = min((<span class="keyword">int</span>)(operandNum/<span class="number">2</span>), (<span class="keyword">int</span>)(mt()%<span class="number">3</span> + <span class="number">2</span>));</span><br><span class="line">    <span class="comment">// 随机生成运算符的种类</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operatorNum; ++i) operators[i] = mt()%<span class="number">4</span> + <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 随机生成括号位置</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; bracketNum*<span class="number">2</span>; ++i)&#123;</span><br><span class="line">        <span class="keyword">int</span> x = mt()%operandNum;</span><br><span class="line">        <span class="keyword">while</span>(hasBracket[x] != <span class="number">-1</span>) x = mt()%operandNum;</span><br><span class="line">        hasBracket[x] = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据相对位置确定括号为左括号还是右括号</span></span><br><span class="line">    <span class="keyword">bool</span> lf = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">-1</span>) <span class="keyword">continue</span>;</span><br><span class="line">        <span class="keyword">if</span>(lf) hasBracket[i] = <span class="number">14</span>;</span><br><span class="line">        <span class="keyword">else</span> hasBracket[i] = <span class="number">15</span>;</span><br><span class="line">        lf = (!lf);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 到这里已经将等式预处理成了(a+b)/c*(d-e)的类似形式</span></span><br><span class="line">    <span class="comment">// 预处理结束之后，我们就要尝试将 a b c d e 确定为具体的数</span></span><br><span class="line">    <span class="keyword">if</span>(!getOperands()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在获取完运算数之后，便可以输出我们得到的等式了</span></span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">13</span>) <span class="built_in">printf</span>(<span class="string">"÷"</span>);</span><br><span class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%c"</span>, op[operators[i - <span class="number">1</span>]]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[i]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>) <span class="built_in">printf</span>(<span class="string">")"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"=%d\n"</span>, ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">// 初始化函数零：初始化运算符 id 符号 以及运算优先级，并且重定向输出</span></span><br><span class="line">    init0();</span><br><span class="line">    <span class="comment">//读入需要生成的运算式数量</span></span><br><span class="line">    <span class="keyword">int</span> n;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);</span><br><span class="line">    <span class="comment">//输出学号及运算式</span></span><br><span class="line">    <span class="built_in">puts</span>(<span class="string">"2017012449"</span>);</span><br><span class="line">    <span class="keyword">while</span>(n--) <span class="keyword">while</span>(!solve()) ;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-3-源代码-4-0"><a href="#4-3-源代码-4-0" class="headerlink" title="4.3 源代码 4.0"></a>4.3 源代码 4.0</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function">mt19937 <span class="title">mt</span><span class="params">(time(<span class="number">0</span>))</span></span>; <span class="comment">// 随机数生成器</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">char</span>&gt; op; <span class="comment">// 运算符id及其符号映射</span></span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; pri; <span class="comment">// 运算符id及其优先级映射</span></span><br><span class="line"><span class="comment">// 10表示加号   运算优先级为 0</span></span><br><span class="line"><span class="comment">// 11表示减号   运算优先级为 0</span></span><br><span class="line"><span class="comment">// 12表示乘号   运算优先级为 1</span></span><br><span class="line"><span class="comment">// 13表示除号   运算优先级为 1</span></span><br><span class="line"><span class="comment">// 14表示左括号 运算优先级为 2</span></span><br><span class="line"><span class="comment">// 15表示右括号 运算优先级为 2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 运算数结构体，id为运算数的位置，val为运算数的值</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> id, val;</span><br><span class="line">    node(<span class="keyword">int</span> id = <span class="number">-1</span>, <span class="keyword">int</span> val = <span class="number">-1</span>):id(id), val(val)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// tot：逆波兰表达式的长度，operatorNum：运算符个数</span></span><br><span class="line"><span class="comment">// operandNum：运算数的个数，bracketNum：括号对数，ans：运算式最终答案</span></span><br><span class="line"><span class="comment">// hasBracket：某个数字处是否有括号，-1表示无括号，14表示左括号，15表示右括号</span></span><br><span class="line"><span class="comment">// operators：对应位置的运算符种类 operands：表示对应位置运算数的值</span></span><br><span class="line"><span class="comment">// RPN：存储逆波兰表达式 fac：存储某个数的约数</span></span><br><span class="line"><span class="comment">// opr：中缀表达式转后缀表达式时的运算符栈</span></span><br><span class="line"><span class="comment">// opd：中缀表达式转后缀表达式时的运算数栈</span></span><br><span class="line"><span class="comment">// numerators：对应位置运算数的分子 denominators：对应位置运算式的分母</span></span><br><span class="line"><span class="keyword">int</span> tot, operatorNum, operandNum, bracketNum, ans;</span><br><span class="line"><span class="keyword">int</span> hasBracket[<span class="number">15</span>], operators[<span class="number">15</span>], operands[<span class="number">15</span>];</span><br><span class="line"><span class="keyword">int</span> RPN[<span class="number">55</span>], fac[<span class="number">105</span>], numerators[<span class="number">15</span>], denominators[<span class="number">15</span>];</span><br><span class="line"><span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; opr;</span><br><span class="line"><span class="built_in">stack</span>&lt;node&gt; opd;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数零：初始化运算符 id 符号 以及运算优先级，并且重定向输出</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init0</span><span class="params">()</span></span>&#123;</span><br><span class="line">    op[<span class="number">10</span>] = <span class="string">'+'</span>, op[<span class="number">11</span>] = <span class="string">'-'</span>;</span><br><span class="line">    op[<span class="number">12</span>] = <span class="string">'*'</span>;</span><br><span class="line">    op[<span class="number">14</span>] = <span class="string">'('</span>, op[<span class="number">15</span>] = <span class="string">')'</span>;</span><br><span class="line">    pri[<span class="number">10</span>] = <span class="number">0</span>, pri[<span class="number">11</span>] = <span class="number">0</span>;</span><br><span class="line">    pri[<span class="number">12</span>] = <span class="number">1</span>, pri[<span class="number">13</span>] = <span class="number">1</span>;</span><br><span class="line">    pri[<span class="number">14</span>] = <span class="number">2</span>, pri[<span class="number">15</span>] = <span class="number">2</span>;</span><br><span class="line">    freopen(<span class="string">"../result.txt"</span>, <span class="string">"w"</span>, <span class="built_in">stdout</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数一：在每次调用solve()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init1</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">15</span>; ++i)&#123;</span><br><span class="line">        operators[i] = <span class="number">-1</span>;</span><br><span class="line">        operands[i] = <span class="number">-1</span>;</span><br><span class="line">        hasBracket[i] = <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数二：在每次调用getOperands()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init2</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数三：在每次调用solve2()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init3</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">15</span>; ++i)&#123;</span><br><span class="line">        operators[i] = <span class="number">-1</span>;</span><br><span class="line">        numerators[i] = <span class="number">-1</span>;</span><br><span class="line">        denominators[i] = <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取运算数的值的函数，如果返回 true 则表示获取成功，否则表示获取失败</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">getOperands</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init2();</span><br><span class="line">    <span class="comment">// 将中缀表达式转换成后缀表达式（也就是逆波兰表达式）</span></span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) opr.push(hasBracket[<span class="number">0</span>]);</span><br><span class="line">    RPN[tot++] = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">while</span>(<span class="literal">true</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(opr.empty() || opr.top() == <span class="number">14</span> || pri[operators[i - <span class="number">1</span>]] &gt; pri[opr.top()])&#123;</span><br><span class="line">                opr.push(operators[i - <span class="number">1</span>]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            RPN[tot++] = opr.top();</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>)&#123;</span><br><span class="line">            opr.push(hasBracket[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        RPN[tot++] = i;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>)&#123;</span><br><span class="line">            <span class="keyword">while</span>(opr.top() != <span class="number">14</span>)&#123;</span><br><span class="line">                RPN[tot++] = opr.top();</span><br><span class="line">                opr.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty())&#123;</span><br><span class="line">        RPN[tot++] = opr.top();</span><br><span class="line">        opr.pop();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 转换成逆波兰表达式后便可以进行尝试填数</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; tot; ++i)&#123;</span><br><span class="line">        <span class="comment">// 如果为运算数则随机为其赋值</span></span><br><span class="line">        <span class="keyword">if</span>(RPN[i] &lt; <span class="number">10</span>)&#123;</span><br><span class="line">            <span class="keyword">int</span> x = mt()%<span class="number">66</span> + <span class="number">1</span>;</span><br><span class="line">            operands[RPN[i]] = x;</span><br><span class="line">            opd.push(node(RPN[i], x));</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//如果为除法，要将除数随机分配为被除数的一个因子</span></span><br><span class="line">        <span class="comment">//如果为减法，要注意减数不能大于被减数</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(RPN[i] == <span class="number">13</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(a.val%b.val == <span class="number">0</span>)&#123;</span><br><span class="line">                opd.push(node(<span class="number">-1</span>, a.val/b.val));</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(b.id == <span class="number">-1</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">int</span> cnt = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j &lt;= a.val; ++j)&#123;</span><br><span class="line">                <span class="keyword">if</span>(j &gt;= <span class="number">100</span>) <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">if</span>(a.val%j) <span class="keyword">continue</span>;</span><br><span class="line">                fac[cnt++] = j;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">int</span> x = mt()%cnt;</span><br><span class="line">            operands[b.id] = fac[x];</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, a.val/fac[x]));</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(RPN[i] == <span class="number">11</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">int</span> dt = a.val - b.val;</span><br><span class="line">            <span class="keyword">if</span>(dt &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, dt));</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">10</span>) opd.push(node(<span class="number">-1</span>, a.val + b.val));</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">12</span>) opd.push(node(<span class="number">-1</span>, a.val*b.val));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    ans = opd.top().val; opd.pop();</span><br><span class="line">    <span class="comment">//控制最终运算结果的范围，可根据需要进行调节</span></span><br><span class="line">    <span class="keyword">if</span>(ans &lt; <span class="number">0</span> || ans &gt; <span class="number">1000</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// solve1() 表示生成普通运算式</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">solve1</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init1();</span><br><span class="line">    <span class="comment">// 随机生成运算符的个数 3~5 ，及运算数个数 4~6</span></span><br><span class="line">    operatorNum = mt()%<span class="number">3</span> + <span class="number">3</span>;</span><br><span class="line">    operandNum = operatorNum + <span class="number">1</span>;</span><br><span class="line">    <span class="comment">// 随机生成括号个数</span></span><br><span class="line">    bracketNum = min((<span class="keyword">int</span>)(operandNum/<span class="number">2</span>), (<span class="keyword">int</span>)(mt()%<span class="number">3</span> + <span class="number">2</span>));</span><br><span class="line">    <span class="comment">// 随机生成运算符的种类</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operatorNum; ++i) operators[i] = mt()%<span class="number">4</span> + <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 随机生成括号位置</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; bracketNum*<span class="number">2</span>; ++i)&#123;</span><br><span class="line">        <span class="keyword">int</span> x = mt()%operandNum;</span><br><span class="line">        <span class="keyword">while</span>(hasBracket[x] != <span class="number">-1</span>) x = mt()%operandNum;</span><br><span class="line">        hasBracket[x] = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据相对位置确定括号为左括号还是右括号</span></span><br><span class="line">    <span class="keyword">bool</span> lf = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">-1</span>) <span class="keyword">continue</span>;</span><br><span class="line">        <span class="keyword">if</span>(lf) hasBracket[i] = <span class="number">14</span>;</span><br><span class="line">        <span class="keyword">else</span> hasBracket[i] = <span class="number">15</span>;</span><br><span class="line">        lf = (!lf);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 到这里已经将等式预处理成了(a+b)/c*(d-e)的类似形式</span></span><br><span class="line">    <span class="comment">// 预处理结束之后，我们就要尝试将 a b c d e 确定为具体的数</span></span><br><span class="line">    <span class="keyword">if</span>(!getOperands()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在获取完运算数之后，便可以输出我们得到的等式了</span></span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">13</span>) <span class="built_in">printf</span>(<span class="string">"÷"</span>);</span><br><span class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%c"</span>, op[operators[i - <span class="number">1</span>]]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[i]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>) <span class="built_in">printf</span>(<span class="string">")"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"=%d\n"</span>, ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// solve2() 生成真分数运算式</span></span><br><span class="line"><span class="comment">// 由于只要求加减法，因此括号出现与否并不会影响答案</span></span><br><span class="line"><span class="comment">// 故solve2()中无须考虑括号与乘除号</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">solve2</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init3();</span><br><span class="line">    operatorNum = mt()%<span class="number">3</span> + <span class="number">3</span>;</span><br><span class="line">    operandNum = operatorNum + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operatorNum; ++i) operators[i] = mt()%<span class="number">2</span> + <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 生成分子与分母，保证分子严格小于分母</span></span><br><span class="line">    denominators[<span class="number">0</span>] = mt()%<span class="number">66</span> + <span class="number">1</span>;</span><br><span class="line">    numerators[<span class="number">0</span>] = mt()%(max(denominators[<span class="number">0</span>] - <span class="number">22</span>, <span class="number">1</span>)) + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">int</span> g = __gcd(numerators[<span class="number">0</span>], denominators[<span class="number">0</span>]);</span><br><span class="line">    numerators[<span class="number">0</span>] /= g, denominators[<span class="number">0</span>] /= g;</span><br><span class="line">    <span class="keyword">int</span> nowNume = numerators[<span class="number">0</span>], nowDeno = denominators[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        denominators[i] = mt()%<span class="number">66</span> + <span class="number">1</span>;</span><br><span class="line">        numerators[i] = mt()%(max(denominators[i] - <span class="number">22</span>, <span class="number">1</span>)) + <span class="number">1</span>;</span><br><span class="line">        g = __gcd(numerators[i], denominators[i]);</span><br><span class="line">        numerators[i] /= g, denominators[i] /= g;</span><br><span class="line">        g = __gcd(denominators[i], nowDeno);</span><br><span class="line">        <span class="keyword">int</span> lcm = nowDeno*denominators[i]/g;</span><br><span class="line">        nowNume *= denominators[i]/g;</span><br><span class="line">        <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">10</span>)&#123;</span><br><span class="line">            nowNume += numerators[i]*nowDeno/g;</span><br><span class="line">            <span class="keyword">if</span>(nowNume &gt;= nowDeno) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">11</span>)&#123;</span><br><span class="line">            nowNume -= numerators[i]*nowDeno/g;</span><br><span class="line">            <span class="keyword">if</span>(nowNume &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        nowDeno = lcm;</span><br><span class="line">        g = __gcd(nowNume, nowDeno);</span><br><span class="line">        nowNume /= g, nowDeno /= g;</span><br><span class="line">        <span class="comment">// 当运算过程中分子分母大于阈值（这里是 666 ），则重新生成算式</span></span><br><span class="line">        <span class="keyword">if</span>(nowNume &gt; <span class="number">666</span> || nowDeno &gt; <span class="number">666</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%d/%d"</span>, numerators[<span class="number">0</span>], denominators[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%c"</span>, op[operators[i - <span class="number">1</span>]]);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d/%d"</span>, numerators[i], denominators[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"=%d/%d\n"</span>, nowNume, nowDeno);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 这里 x 与模数控制分数运算式出现的概率</span></span><br><span class="line"><span class="comment">// 这里限定为 30% 的分数运算式， 70% 的普通运算式</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> x = mt()%<span class="number">10</span>;</span><br><span class="line">    <span class="keyword">if</span>(x &lt;= <span class="number">2</span>) <span class="keyword">while</span>(!solve2()) ;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">while</span>(!solve1()) ;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">// 初始化函数零：初始化运算符 id 符号 以及运算优先级，并且重定向输出</span></span><br><span class="line">    init0();</span><br><span class="line">    <span class="comment">//读入需要生成的运算式数量</span></span><br><span class="line">    <span class="keyword">int</span> n;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);</span><br><span class="line">    <span class="comment">//输出学号及运算式</span></span><br><span class="line">    <span class="built_in">puts</span>(<span class="string">"2017012449"</span>);</span><br><span class="line">    <span class="keyword">while</span>(n--) solve();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-4-代码-5-0"><a href="#4-4-代码-5-0" class="headerlink" title="4.4 代码 5.0"></a>4.4 代码 5.0</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function">mt19937 <span class="title">mt</span><span class="params">(time(<span class="number">0</span>))</span></span>; <span class="comment">// 随机数生成器</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">char</span>&gt; op; <span class="comment">// 运算符id及其符号映射</span></span><br><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; pri; <span class="comment">// 运算符id及其优先级映射</span></span><br><span class="line"><span class="comment">// 10表示加号   运算优先级为 0</span></span><br><span class="line"><span class="comment">// 11表示减号   运算优先级为 0</span></span><br><span class="line"><span class="comment">// 12表示乘号   运算优先级为 1</span></span><br><span class="line"><span class="comment">// 13表示除号   运算优先级为 1</span></span><br><span class="line"><span class="comment">// 14表示左括号</span></span><br><span class="line"><span class="comment">// 15表示右括号</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 运算数结构体，id为运算数的位置，val为运算数的值</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> id, val;</span><br><span class="line">    node(<span class="keyword">int</span> id = <span class="number">-1</span>, <span class="keyword">int</span> val = <span class="number">-1</span>):id(id), val(val)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// tot：逆波兰表达式的长度，operatorNum：运算符个数</span></span><br><span class="line"><span class="comment">// operandNum：运算数的个数，bracketNum：括号对数，ans：运算式最终答案</span></span><br><span class="line"><span class="comment">// hasBracket：某个数字处是否有括号，-1表示无括号，14表示左括号，15表示右括号</span></span><br><span class="line"><span class="comment">// operators：对应位置的运算符种类 operands：表示对应位置运算数的值</span></span><br><span class="line"><span class="comment">// RPN：存储逆波兰表达式 fac：存储某个数的约数</span></span><br><span class="line"><span class="comment">// opr：中缀表达式转后缀表达式时的运算符栈</span></span><br><span class="line"><span class="comment">// opd：中缀表达式转后缀表达式时的运算数栈</span></span><br><span class="line"><span class="comment">// numerators：对应位置运算数的分子 denominators：对应位置运算式的分母</span></span><br><span class="line"><span class="keyword">int</span> tot, operatorNum, operandNum, bracketNum, ans;</span><br><span class="line"><span class="keyword">int</span> hasBracket[<span class="number">15</span>], operators[<span class="number">15</span>], operands[<span class="number">15</span>];</span><br><span class="line"><span class="keyword">int</span> RPN[<span class="number">55</span>], fac[<span class="number">105</span>], numerators[<span class="number">15</span>], denominators[<span class="number">15</span>];</span><br><span class="line"><span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; opr;</span><br><span class="line"><span class="built_in">stack</span>&lt;node&gt; opd;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数零：初始化运算符 id 符号 以及运算优先级，并且重定向输出</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init0</span><span class="params">()</span></span>&#123;</span><br><span class="line">    op[<span class="number">10</span>] = <span class="string">'+'</span>, op[<span class="number">11</span>] = <span class="string">'-'</span>;</span><br><span class="line">    op[<span class="number">12</span>] = <span class="string">'*'</span>;</span><br><span class="line">    op[<span class="number">14</span>] = <span class="string">'('</span>, op[<span class="number">15</span>] = <span class="string">')'</span>;</span><br><span class="line">    pri[<span class="number">10</span>] = <span class="number">0</span>, pri[<span class="number">11</span>] = <span class="number">0</span>;</span><br><span class="line">    pri[<span class="number">12</span>] = <span class="number">1</span>, pri[<span class="number">13</span>] = <span class="number">1</span>;</span><br><span class="line">    freopen(<span class="string">"../result.txt"</span>, <span class="string">"w"</span>, <span class="built_in">stdout</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数一：在每次调用solve()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init1</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">15</span>; ++i)&#123;</span><br><span class="line">        operators[i] = <span class="number">-1</span>;</span><br><span class="line">        operands[i] = <span class="number">-1</span>;</span><br><span class="line">        hasBracket[i] = <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数二：在每次调用getOperands()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init2</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty()) opr.pop();</span><br><span class="line">    <span class="keyword">while</span>(!opd.empty()) opd.pop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化函数三：在每次调用solve2()时进行初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init3</span><span class="params">()</span></span>&#123;</span><br><span class="line">    tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">15</span>; ++i)&#123;</span><br><span class="line">        operators[i] = <span class="number">-1</span>;</span><br><span class="line">        numerators[i] = <span class="number">-1</span>;</span><br><span class="line">        denominators[i] = <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 生成一个范围在 [l, r] 中的随机数</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getNum</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> ret = mt()%(r - l + <span class="number">1</span>) + l;</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取运算数的值的函数，如果返回 true 则表示获取成功，否则表示获取失败</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">getOperands</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init2();</span><br><span class="line">    <span class="comment">// 将中缀表达式转换成后缀表达式（也就是逆波兰表达式）</span></span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) opr.push(hasBracket[<span class="number">0</span>]);</span><br><span class="line">    RPN[tot++] = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">while</span>(<span class="literal">true</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(opr.empty() || opr.top() == <span class="number">14</span> || pri[operators[i - <span class="number">1</span>]] &gt; pri[opr.top()])&#123;</span><br><span class="line">                opr.push(operators[i - <span class="number">1</span>]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            RPN[tot++] = opr.top();</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>)&#123;</span><br><span class="line">            opr.push(hasBracket[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        RPN[tot++] = i;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>)&#123;</span><br><span class="line">            <span class="keyword">while</span>(opr.top() != <span class="number">14</span>)&#123;</span><br><span class="line">                RPN[tot++] = opr.top();</span><br><span class="line">                opr.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            opr.pop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(!opr.empty())&#123;</span><br><span class="line">        RPN[tot++] = opr.top();</span><br><span class="line">        opr.pop();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 转换成逆波兰表达式后便可以进行尝试填数</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; tot; ++i)&#123;</span><br><span class="line">        <span class="comment">// 如果为运算数则随机为其赋值</span></span><br><span class="line">        <span class="keyword">if</span>(RPN[i] &lt; <span class="number">10</span>)&#123;</span><br><span class="line">            <span class="keyword">int</span> x = getNum(<span class="number">1</span>, <span class="number">66</span>);</span><br><span class="line">            operands[RPN[i]] = x;</span><br><span class="line">            opd.push(node(RPN[i], x));</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//如果为除法，要将除数随机分配为被除数的一个因子</span></span><br><span class="line">        <span class="comment">//如果为减法，要注意减数不能大于被减数</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(RPN[i] == <span class="number">13</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(a.val%b.val == <span class="number">0</span>)&#123;</span><br><span class="line">                opd.push(node(<span class="number">-1</span>, a.val/b.val));</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(b.id == <span class="number">-1</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">int</span> cnt = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j &lt;= a.val; ++j)&#123;</span><br><span class="line">                <span class="keyword">if</span>(j &gt;= <span class="number">100</span>) <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">if</span>(a.val%j) <span class="keyword">continue</span>;</span><br><span class="line">                fac[cnt++] = j;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">int</span> x =getNum(<span class="number">0</span>, cnt - <span class="number">1</span>);</span><br><span class="line">            operands[b.id] = fac[x];</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, a.val/fac[x]));</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(RPN[i] == <span class="number">11</span>)&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">int</span> dt = a.val - b.val;</span><br><span class="line">            <span class="keyword">if</span>(dt &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            opd.push(node(<span class="number">-1</span>, dt));</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            node b = opd.top(); opd.pop();</span><br><span class="line">            node a = opd.top(); opd.pop();</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">10</span>) opd.push(node(<span class="number">-1</span>, a.val + b.val));</span><br><span class="line">            <span class="keyword">if</span>(RPN[i] == <span class="number">12</span>) opd.push(node(<span class="number">-1</span>, a.val*b.val));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    ans = opd.top().val; opd.pop();</span><br><span class="line">    <span class="comment">//控制最终运算结果的范围，可根据需要进行调节</span></span><br><span class="line">    <span class="keyword">if</span>(ans &lt; <span class="number">0</span> || ans &gt; <span class="number">1000</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// solve1() 表示生成普通运算式</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">solve1</span><span class="params">(<span class="keyword">bool</span> flag)</span></span>&#123;</span><br><span class="line">    init1();</span><br><span class="line">    <span class="comment">// 随机生成运算符的个数 3~5 ，及运算数个数 4~6</span></span><br><span class="line">    operatorNum = getNum(<span class="number">3</span>, <span class="number">5</span>);</span><br><span class="line">    operandNum = operatorNum + <span class="number">1</span>;</span><br><span class="line">    <span class="comment">// 随机生成括号个数</span></span><br><span class="line">    bracketNum = min((<span class="keyword">int</span>)(operandNum/<span class="number">2</span>), getNum(<span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">    <span class="keyword">if</span>(flag) bracketNum = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">// 随机生成运算符的种类</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operatorNum; ++i) operators[i] = getNum(<span class="number">10</span>, <span class="number">13</span>);</span><br><span class="line">    <span class="comment">// 随机生成括号位置</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; bracketNum*<span class="number">2</span>; ++i)&#123;</span><br><span class="line">        <span class="keyword">int</span> x = getNum(<span class="number">0</span>, operandNum - <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">while</span>(hasBracket[x] != <span class="number">-1</span>) x = getNum(<span class="number">0</span>, operandNum - <span class="number">1</span>);</span><br><span class="line">        hasBracket[x] = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据相对位置确定括号为左括号还是右括号</span></span><br><span class="line">    <span class="keyword">bool</span> lf = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">-1</span>) <span class="keyword">continue</span>;</span><br><span class="line">        <span class="keyword">if</span>(lf) hasBracket[i] = <span class="number">14</span>;</span><br><span class="line">        <span class="keyword">else</span> hasBracket[i] = <span class="number">15</span>;</span><br><span class="line">        lf = (!lf);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 到这里已经将等式预处理成了(a+b)/c*(d-e)的类似形式</span></span><br><span class="line">    <span class="comment">// 预处理结束之后，我们就要尝试将 a b c d e 确定为具体的数</span></span><br><span class="line">    <span class="keyword">if</span>(!getOperands()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在获取完运算数之后，便可以输出我们得到的等式了</span></span><br><span class="line">    <span class="keyword">if</span>(hasBracket[<span class="number">0</span>] != <span class="number">-1</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">13</span>) <span class="built_in">printf</span>(<span class="string">"÷"</span>);</span><br><span class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%c"</span>, op[operators[i - <span class="number">1</span>]]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">14</span>) <span class="built_in">printf</span>(<span class="string">"("</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d"</span>, operands[i]);</span><br><span class="line">        <span class="keyword">if</span>(hasBracket[i] == <span class="number">15</span>) <span class="built_in">printf</span>(<span class="string">")"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"=%d\n"</span>, ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// solve2() 生成真分数运算式</span></span><br><span class="line"><span class="comment">// 由于只要求加减法，因此括号出现与否并不会影响答案</span></span><br><span class="line"><span class="comment">// 故solve2()中无须考虑括号与乘除号</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">solve2</span><span class="params">()</span></span>&#123;</span><br><span class="line">    init3();</span><br><span class="line">    operatorNum = getNum(<span class="number">3</span>, <span class="number">5</span>);</span><br><span class="line">    operandNum = operatorNum + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operatorNum; ++i) operators[i] = getNum(<span class="number">10</span>, <span class="number">11</span>);</span><br><span class="line">    <span class="comment">// 生成分子与分母，保证分子严格小于分母</span></span><br><span class="line">    denominators[<span class="number">0</span>] = getNum(<span class="number">1</span>, <span class="number">66</span>);</span><br><span class="line">    numerators[<span class="number">0</span>] = getNum(<span class="number">1</span>, max(denominators[<span class="number">0</span>] - <span class="number">22</span>, <span class="number">1</span>));</span><br><span class="line">    <span class="keyword">int</span> g = __gcd(numerators[<span class="number">0</span>], denominators[<span class="number">0</span>]);</span><br><span class="line">    numerators[<span class="number">0</span>] /= g, denominators[<span class="number">0</span>] /= g;</span><br><span class="line">    <span class="keyword">int</span> nowNume = numerators[<span class="number">0</span>], nowDeno = denominators[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        denominators[i] = getNum(<span class="number">1</span>, <span class="number">66</span>);</span><br><span class="line">        numerators[i] =  getNum(<span class="number">1</span>, max(denominators[<span class="number">0</span>] - <span class="number">22</span>, <span class="number">1</span>));</span><br><span class="line">        g = __gcd(numerators[i], denominators[i]);</span><br><span class="line">        numerators[i] /= g, denominators[i] /= g;</span><br><span class="line">        g = __gcd(denominators[i], nowDeno);</span><br><span class="line">        <span class="keyword">int</span> lcm = nowDeno*denominators[i]/g;</span><br><span class="line">        nowNume *= denominators[i]/g;</span><br><span class="line">        <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">10</span>)&#123;</span><br><span class="line">            nowNume += numerators[i]*nowDeno/g;</span><br><span class="line">            <span class="keyword">if</span>(nowNume &gt;= nowDeno) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(operators[i - <span class="number">1</span>] == <span class="number">11</span>)&#123;</span><br><span class="line">            nowNume -= numerators[i]*nowDeno/g;</span><br><span class="line">            <span class="keyword">if</span>(nowNume &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        nowDeno = lcm;</span><br><span class="line">        g = __gcd(nowNume, nowDeno);</span><br><span class="line">        nowNume /= g, nowDeno /= g;</span><br><span class="line">        <span class="comment">// 当运算过程中分子分母大于阈值（这里是 666 ），则重新生成算式</span></span><br><span class="line">        <span class="keyword">if</span>(nowNume &gt; <span class="number">666</span> || nowDeno &gt; <span class="number">666</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%d/%d"</span>, numerators[<span class="number">0</span>], denominators[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; operandNum; ++i)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%c"</span>, op[operators[i - <span class="number">1</span>]]);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d/%d"</span>, numerators[i], denominators[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"=%d/%d\n"</span>, nowNume, nowDeno);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 这里 x 与模数控制分数运算式出现的概率</span></span><br><span class="line"><span class="comment">// 这里限定为 30% 的分数运算式， 50% 的有括号普通运算式 ，20% 的无括号普通运算式</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> x = getNum(<span class="number">0</span>, <span class="number">9</span>);</span><br><span class="line">    <span class="keyword">if</span>(x &lt;= <span class="number">2</span>) <span class="keyword">while</span>(!solve2()) ;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span>(x &lt;= <span class="number">7</span>) <span class="keyword">while</span>(!solve1(<span class="literal">true</span>)) ;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">while</span>(!solve1(<span class="literal">false</span>)) ;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">// 初始化函数零：初始化运算符 id 符号 以及运算优先级，并且重定向输出</span></span><br><span class="line">    init0();</span><br><span class="line">    <span class="comment">//读入需要生成的运算式数量</span></span><br><span class="line">    <span class="keyword">int</span> n;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);</span><br><span class="line">    <span class="comment">//输出学号及运算式</span></span><br><span class="line">    <span class="built_in">puts</span>(<span class="string">"2017012449"</span>);</span><br><span class="line">    <span class="keyword">while</span>(n--) solve();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="5-演示图片"><a href="#5-演示图片" class="headerlink" title="5 演示图片"></a>5 演示图片</h1><p><img src="/20190319/Arithmetic-Device/p1.png" alt="20"></p>
<p><img src="/20190319/Arithmetic-Device/p2.png" alt="30"></p>
<h1 id="6-个人软件过程-PSP"><a href="#6-个人软件过程-PSP" class="headerlink" title="6 个人软件过程 PSP"></a>6 个人软件过程 PSP</h1><h2 id="6-1-代码1-0-PSP"><a href="#6-1-代码1-0-PSP" class="headerlink" title="6.1 代码1.0 PSP"></a>6.1 代码1.0 PSP</h2><h3 id="6-1-1-已实现功能"><a href="#6-1-1-已实现功能" class="headerlink" title="6.1.1 已实现功能"></a>6.1.1 已实现功能</h3><ul>
<li><p>1 生成n道数字在 0~100 之间的算术题</p>
</li>
<li><p>2 运算过程中不出现分数与负数</p>
</li>
<li><p>3 运行 .exe 生成 result.txt</p>
</li>
<li><p>4 支持有括号的运算</p>
</li>
</ul>
<h3 id="6-1-2-未完成功能"><a href="#6-1-2-未完成功能" class="headerlink" title="6.1.2 未完成功能"></a>6.1.2 未完成功能</h3><ul>
<li>1 支持真分数的出题与运算</li>
</ul>
<h3 id="6-1-3-PSP表格"><a href="#6-1-3-PSP表格" class="headerlink" title="6.1.3 PSP表格"></a>6.1.3 PSP表格</h3><p><img src="/20190319/Arithmetic-Device/p3.png" alt="PSP2.1"></p>
<h2 id="6-2-代码2-0-PSP"><a href="#6-2-代码2-0-PSP" class="headerlink" title="6.2 代码2.0 PSP"></a>6.2 代码2.0 PSP</h2><h3 id="6-2-1"><a href="#6-2-1" class="headerlink" title="6.2.1"></a>6.2.1</h3><h3 id="6-2-1-更新内容"><a href="#6-2-1-更新内容" class="headerlink" title="6.2.1 更新内容"></a>6.2.1 更新内容</h3><ul>
<li><p>重新复审了代码</p>
</li>
<li><p>优化了代码格式</p>
</li>
<li><p>分段加入了注释</p>
</li>
<li><p>修复了一些小BUG</p>
</li>
</ul>
<h3 id="6-2-2-PSP表格"><a href="#6-2-2-PSP表格" class="headerlink" title="6.2.2 PSP表格"></a>6.2.2 PSP表格</h3><p><img src="/20190319/Arithmetic-Device/p4.png" alt="PSP"></p>
<h2 id="6-3-代码3-0-PSP"><a href="#6-3-代码3-0-PSP" class="headerlink" title="6.3 代码3.0 PSP"></a>6.3 代码3.0 PSP</h2><h3 id="6-3-1-更新内容"><a href="#6-3-1-更新内容" class="headerlink" title="6.3.1 更新内容"></a>6.3.1 更新内容</h3><p>解决了被除数可能会大于100的问题</p>
<p><img src="/20190319/Arithmetic-Device/p5.png" alt="问题"></p>
<p>优化了代码框架</p>
<h3 id="6-3-2-PSP表格"><a href="#6-3-2-PSP表格" class="headerlink" title="6.3.2 PSP表格"></a>6.3.2 PSP表格</h3><p><img src="/20190319/Arithmetic-Device/p6.png" alt="PSP"></p>
<h2 id="6-4-代码4-0-PSP"><a href="#6-4-代码4-0-PSP" class="headerlink" title="6.4 代码4.0 PSP"></a>6.4 代码4.0 PSP</h2><h3 id="6-4-1-更新内容"><a href="#6-4-1-更新内容" class="headerlink" title="6.4.1 更新内容"></a>6.4.1 更新内容</h3><p>成功生成真分数运算式</p>
<p><img src="/20190319/Arithmetic-Device/p7.png" alt="源代码 4.0"></p>
<h3 id="6-4-2-PSP表格"><a href="#6-4-2-PSP表格" class="headerlink" title="6.4.2 PSP表格"></a>6.4.2 PSP表格</h3><p><img src="/20190319/Arithmetic-Device/p8.png" alt="PSP"></p>
<h2 id="6-5-代码5-0-PSP"><a href="#6-5-代码5-0-PSP" class="headerlink" title="6.5 代码5.0 PSP"></a>6.5 代码5.0 PSP</h2><h3 id="6-5-1-更新内容"><a href="#6-5-1-更新内容" class="headerlink" title="6.5.1 更新内容"></a>6.5.1 更新内容</h3><ul>
<li><p>封装了随机数生成函数</p>
</li>
<li><p>随机生成无括号运算式</p>
</li>
</ul>
<p><img src="/20190319/Arithmetic-Device/p9.png" alt="5.0"></p>
<h3 id="6-5-2-PSP表格"><a href="#6-5-2-PSP表格" class="headerlink" title="6.5.2 PSP表格"></a>6.5.2 PSP表格</h3><p><img src="/20190319/Arithmetic-Device/p10.png" alt="PSP"></p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/软件工程导论/" rel="tag"># 软件工程导论</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/20190217/Cayley-s-formula-And-Prufer-sequences/" rel="next" title="Cayley's formula And Prüfer sequences">
                <i class="fa fa-chevron-left"></i> Cayley's formula And Prüfer sequences
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/20191125/思辨与创新-第一章-提出问题：如何提出一个新的、好的问题？/" rel="prev" title="思辨与创新 第一章 提出问题：如何提出一个新的、好的问题？">
                思辨与创新 第一章 提出问题：如何提出一个新的、好的问题？ <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/star.jpg" alt="linxi">
            
              <p class="site-author-name" itemprop="name">linxi</p>
              <p class="site-description motion-element" itemprop="description">stay hungry, stay foolish</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">15</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">9</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">13</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/linxilinxilinxi" title="https://blog.csdn.net/linxilinxilinxi" rel="noopener" target="_blank">My CSDN</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-任务描述"><span class="nav-number">1.</span> <span class="nav-text">1 任务描述</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-概述"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-基本要求"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 基本要求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-附加功能要求"><span class="nav-number">1.3.</span> <span class="nav-text">1.3 附加功能要求</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-项目地址"><span class="nav-number">2.</span> <span class="nav-text">2 项目地址</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-项目源代码"><span class="nav-number">3.</span> <span class="nav-text">3 项目源代码</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-更新版本"><span class="nav-number">4.</span> <span class="nav-text">4 更新版本</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#4-1-源代码-2-0"><span class="nav-number">4.1.</span> <span class="nav-text">4.1 源代码 2.0</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-2-源代码-3-0"><span class="nav-number">4.2.</span> <span class="nav-text">4.2 源代码 3.0</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-3-源代码-4-0"><span class="nav-number">4.3.</span> <span class="nav-text">4.3 源代码 4.0</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-4-代码-5-0"><span class="nav-number">4.4.</span> <span class="nav-text">4.4 代码 5.0</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-演示图片"><span class="nav-number">5.</span> <span class="nav-text">5 演示图片</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6-个人软件过程-PSP"><span class="nav-number">6.</span> <span class="nav-text">6 个人软件过程 PSP</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#6-1-代码1-0-PSP"><span class="nav-number">6.1.</span> <span class="nav-text">6.1 代码1.0 PSP</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-1-已实现功能"><span class="nav-number">6.1.1.</span> <span class="nav-text">6.1.1 已实现功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-2-未完成功能"><span class="nav-number">6.1.2.</span> <span class="nav-text">6.1.2 未完成功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-3-PSP表格"><span class="nav-number">6.1.3.</span> <span class="nav-text">6.1.3 PSP表格</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-2-代码2-0-PSP"><span class="nav-number">6.2.</span> <span class="nav-text">6.2 代码2.0 PSP</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-1"><span class="nav-number">6.2.1.</span> <span class="nav-text">6.2.1</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-1-更新内容"><span class="nav-number">6.2.2.</span> <span class="nav-text">6.2.1 更新内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-2-PSP表格"><span class="nav-number">6.2.3.</span> <span class="nav-text">6.2.2 PSP表格</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-3-代码3-0-PSP"><span class="nav-number">6.3.</span> <span class="nav-text">6.3 代码3.0 PSP</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-3-1-更新内容"><span class="nav-number">6.3.1.</span> <span class="nav-text">6.3.1 更新内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-3-2-PSP表格"><span class="nav-number">6.3.2.</span> <span class="nav-text">6.3.2 PSP表格</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-4-代码4-0-PSP"><span class="nav-number">6.4.</span> <span class="nav-text">6.4 代码4.0 PSP</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-4-1-更新内容"><span class="nav-number">6.4.1.</span> <span class="nav-text">6.4.1 更新内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-4-2-PSP表格"><span class="nav-number">6.4.2.</span> <span class="nav-text">6.4.2 PSP表格</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-5-代码5-0-PSP"><span class="nav-number">6.5.</span> <span class="nav-text">6.5 代码5.0 PSP</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-5-1-更新内容"><span class="nav-number">6.5.1.</span> <span class="nav-text">6.5.1 更新内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-5-2-PSP表格"><span class="nav-number">6.5.2.</span> <span class="nav-text">6.5.2 PSP表格</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2019 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">linxi</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.0.0</div>





<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">全站共 65.5k 字</span>
</div>

        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>














  
    
    
  
  <script color="25,202,173" opacity="0.8" zindex="-1" count="200" src="/lib/canvas-nest/canvas-nest.min.js"></script>













  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=7.0.0"></script>

  <script src="/js/src/motion.js?v=7.0.0"></script>



  
  


  <script src="/js/src/affix.js?v=7.0.0"></script>

  <script src="/js/src/schemes/pisces.js?v=7.0.0"></script>



  
  <script src="/js/src/scrollspy.js?v=7.0.0"></script>
<script src="/js/src/post-details.js?v=7.0.0"></script>



  


  <script src="/js/src/bootstrap.js?v=7.0.0"></script>



  
  

<script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script>



<script src="//unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail,link';
  guest = guest.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: false,
    notify: false,
    appId: 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz',
    appKey: 'akTaTqH7XeeKc6CPRfB7vbDl',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false
  });
</script>




  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  
  
  <script>
    
    function addCount(Counter) {
      var $visitors = $('.leancloud_visitors');
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();

      Counter('get', '/classes/Counter', { where: JSON.stringify({ url }) })
        .done(function({ results }) {
          if (results.length > 0) {
            var counter = results[0];
            
            Counter('put', '/classes/Counter/' + counter.objectId, JSON.stringify({ time: { '__op': 'Increment', 'amount': 1 } }))
            
              .done(function() {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.time + 1);
              })
            
              .fail(function ({ responseJSON }) {
                console.log(`Failed to save Visitor num, with error message: ${responseJSON.error}`);
              })
          } else {
            
              Counter('post', '/classes/Counter', JSON.stringify({ title: title, url: url, time: 1 }))
                .done(function() {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(1);
                })
                .fail(function() {
                  console.log('Failed to create');
                });
            
          }
        })
        .fail(function ({ responseJSON }) {
          console.log(`LeanCloud Counter Error: ${responseJSON.code} ${responseJSON.error}`);
        });
    }
    

    $(function() {
      $.get('https://app-router.leancloud.cn/2/route?appId=' + 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz')
        .done(function({ api_server }) {
          var Counter = function(method, url, data) {
            return $.ajax({
              method: method,
              url: 'https://' + api_server + '/1.1' + url,
              headers: {
                'X-LC-Id': 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz',
                'X-LC-Key': 'akTaTqH7XeeKc6CPRfB7vbDl',
                'Content-Type': 'application/json',
              },
              data: data
            });
          };
          
            addCount(Counter);
          
        });
    });
  </script>



  

  
  

  
  

  
    
      <script type="text/x-mathjax-config">
  

  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true,
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      
      equationNumbers: {
        autoNumber: "AMS"
      }
    }
  });
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
      for (i = 0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>
<script src="//cdn.jsdelivr.net/npm/mathjax@2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

<style>
.MathJax_Display {
  overflow: auto hidden;
}
</style>

    
  


  

  

  

  

  

  

  

  

  

  

</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>

